//报名组件，装上即可使用
const app = getApp()
const db = wx.cloud.database()
const _ = db.command
Component({
  data: {
    on: false,
    phone:"",
    name:"",
  },
  properties: {
    even: Object
  },
  methods: {
    
    //把窗体显示出来
    show(event) {
      this.setData({
        on: true,
        event:event
      })
      console.log(this.data.even)
    },
    shutShare() {
      this.setData({
        on: false,
        name:"",
        phone:""
      })
      this.animate(".span", [{
        opacity: 1
      }, {
        opacity: 0
      }], 100)
      this.animate(".zhezhao", [{
        opacity: 0.6
      }, {
        opacity: 0
      }], 100)
    },
    shut(){
      if(this.data.name===""){
        wx.showToast({
          title: '姓名不能为空',
          icon: 'error',
          duration: 1000,
          mask: true,
        })
        return
      }
      if(this.data.phone.length<8||(!/^\d+$/.test(parseInt(this.data.phone)))){
        wx.showToast({
          title: '手机号格式错误',
          icon: 'error',
          duration: 1000,
          mask: true,
        })
      }else{

        db.collection('community_moreList').doc(this.data.event._id).update({
          data: {
            solitaireName: _.push(this.data.name+'   '+this.data.phone)
          },
          success: (res)=>{
            //报名成功时会通知把新数据加到页面本身就有的数据里而不是刷新页面
            db.collection('community_moreList').doc(this.data.event._id).get({
              success:(res)=>{
                this.triggerEvent('myevent', {solitaireName:res.data.solitaireName})
              }
            })
            wx.showToast({
              title: '报名成功',
              icon: 'success',
              duration: 1000,
              mask: true,
              complete:()=>{
                this.shutShare()
              }
            })
          }
        })
        
      }
    },
    cancle(){
      this.shutShare()
    },
    nothing(){},
    preventdefault(){}
  }
})